/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idv.cloudee.proxy2.impl.goodies;

import idv.cloudee.proxy2.impl.getpost.GETPOSTModifier;
import idv.cloudee.proxy2.impl.getpost.GETPOSTModifierFactory;

/**
 *
 * @author yuxuanhuang
 */
public class GETPOSTStatFactory implements GETPOSTModifierFactory {

    private long upload;
    private long download;
    private final Object uploadLock = new Object();
    private final Object downloadLock = new Object();
    private final Object updateLock = new Object();

    public void waitForUpdate() throws InterruptedException {
        synchronized (updateLock) {
            updateLock.wait();
        }
    }

    private void notifyUpdate() {
        synchronized (updateLock) {
            updateLock.notifyAll();
        }
    }

    public GETPOSTModifier getGETPOSTModifier() {
        return new GETPOSTStat(this);
    }

    public void addUpload(int c) {
        synchronized (uploadLock) {
            upload += c;
            notifyUpdate();
        }
    }

    public void addDownload(int c) {
        synchronized (downloadLock) {
            download += c;
            notifyUpdate();
        }
    }

    public long getDownload() {
        synchronized (downloadLock) {
            return download;
        }
    }

    public long getUpload() {
        synchronized (uploadLock) {
            return upload;
        }
    }
}
